"""
系统管理API路由
"""

from fastapi import APIRouter
from fastapi.responses import HTMLResponse
from typing import Dict, Any

from managers.stripe_manager import StripeManager

router = APIRouter(tags=["系统管理"])

# 创建全局的Stripe管理器实例
stripe_manager = StripeManager()


@router.get("/", response_class=HTMLResponse)
async def read_root():
    """返回主页HTML"""
    try:
        with open("index.html", "r", encoding="utf-8") as f:
            return HTMLResponse(content=f.read())
    except FileNotFoundError:
        return HTMLResponse(content="<h1>Stripe集成演示系统</h1><p>请确保index.html文件存在</p>")


@router.get("/api/status")
async def get_system_status():
    """获取系统状态"""
    return stripe_manager.get_system_status()


@router.post("/api/reload-data")
async def reload_data():
    """重新加载所有数据"""
    stripe_manager.reload_all_data()
    return {"message": "数据重新加载成功"}


@router.post("/api/backup-data")
async def backup_data():
    """备份所有数据"""
    results = stripe_manager.backup_all_data()
    return {
        "message": "数据备份完成",
        "results": results
    }
